package com.tenement.netty.service.impl;

import com.alibaba.fastjson.JSON;
import com.tenement.entity.ChatModel;
import com.tenement.netty.service.ChatHandlerService;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

import java.sql.Timestamp;
import java.util.Date;
import java.util.Map;
import java.util.Set;

public class LineRadioServiceImpl implements ChatHandlerService {
    @Override
    public void execute(ChatModel chatModel, ChannelHandlerContext channelHandlerContext, Map<String, Channel> privateChannel, Set<Channel> channelGroups) {
        chatModel.setType((short)3).setSendTime(new Timestamp(new Date().getTime()));
        channelGroups.forEach( item -> {
            if( item.id() != channelHandlerContext.channel().id() ) {
                item.writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(chatModel)));
            }
        });
    }
}
